package com.banmao.mall.controller.client;


import com.banmao.mall.mapper.CouponMapper;
import com.banmao.mall.model.bo.client.coupon.CouponListBO;
import com.banmao.mall.model.bo.client.coupon.CouponReceiveBO;
import com.banmao.mall.model.bo.client.coupon.ExchangeCodeBO;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.client.collect.CollectListVO;
import com.banmao.mall.model.vo.client.coupon.CouponListVO;
import com.banmao.mall.model.vo.client.index.CouponVO;
import com.banmao.mall.service.client.CollectService;
import com.banmao.mall.service.client.CouponService;
import com.banmao.mall.util.CustomUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("cCouponController")
@RequestMapping("/wx")
public class CouponController {


    @Autowired
    CouponService couponService;

    @Autowired
    CouponMapper couponMapper;




    @RequestMapping("/coupon/list")
    public BaseRespVO couponList(@Validated CouponListBO couponListBO) throws IllegalAccessException {


        /**
         * 1.获取优惠券列表的时候传入当前的时间，与优惠券的截止日期对比，若果过期，进行状态更改
         * 2.获取没有效的优惠券。
         */

        List<CouponVO> couponVOS = couponService.selectCouponsWithPageAndSize(couponListBO.getPage(), couponListBO.getSize());
        CouponListVO couponListVO = new CouponListVO();
        couponListVO.setData(couponVOS);
        return BaseRespVO.ok(couponListVO, "成功", 0);
    }


    @RequestMapping("/coupon/mylist")
    public BaseRespVO couponMyList(@Validated CouponListBO couponListBO) throws IllegalAccessException {

        /**
         * 1.从用户优惠券那里获取该用户所有的状态为0的优惠券的ID
         * 2.从优惠券的表单中获取详细信息。此时不应该判断是否被删除
         */

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        if (userId == null) {
            return BaseRespVO.error("请登录", 501);
        } else {
            List<CouponVO> userCoupons = couponService.getUserCouponByStatus(userId, couponListBO);
            CouponListVO couponListVO = new CouponListVO();
            couponListVO.setData(userCoupons);
            return BaseRespVO.ok(couponListVO, "成功", 0);
        }
    }


    @RequestMapping("/coupon/selectlist")
    public BaseRespVO couponSelectList(Integer cartId,Integer grouponRulesId){
        /**
         * 1.获取该用户所有全场通用的有效的优惠券
         * 2.根据购物车的Id获取购物车中的所有商品ID，再从我的优惠券中获取对应的商品优惠券
         */
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        if (userId == null) {
            return BaseRespVO.error("请登录", 501);
        }else {
            List<CouponVO> couponVOS = couponService.CouponSelectlist(userId,cartId,grouponRulesId);
            CouponListVO couponListVO = new CouponListVO();
            couponListVO.setData(couponVOS);
            return BaseRespVO.ok(couponVOS, "成功", 0);
        }

    }

    @RequestMapping("/coupon/receive")
    public BaseRespVO couponReceive(@RequestBody CouponReceiveBO couponId) {

        /**
         * 1.通过 SecurityUtils获取subject
         * 2.通过subject获取session
         * 3.通过session获取userId
         * 4.判断用户是否登录
         * 5.如果登录，查看优惠券是否为空，空返回响应信息，
         *    如果不为空，查看优惠券可以领的次数。然后查找
         *
         */
        Subject subject = SecurityUtils.getSubject();

        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        if (userId == null) {
            return BaseRespVO.error("请登录", 501);
        } else {
            /**
             * 1.优惠券已经发放完
             * 2.不能再领
             *  （1）已经领过
             *   （2）数量达到上限
             * 3.可以正常添加
             */
            Integer state = couponService.receiveService(userId, couponId.getCouponId());
            if (state == 0) {
                return BaseRespVO.error("优惠券已经领完了", 740);
            } else if (state == 1) {
                return BaseRespVO.ok(0);
            } else {
                return BaseRespVO.error("领券次数达上限", 740);
            }

        }

    }

    @RequestMapping("/coupon/exchange")
    public BaseRespVO couponExchange(@RequestBody ExchangeCodeBO exchangeCodeBO) {
        /**
         * 1.根据type和code查找可以兑换的码
         * 2.进行判断是否可以再领
         */

        Subject subject = SecurityUtils.getSubject();

        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        if (userId == null) {
            return BaseRespVO.error("请登录", 501);
        } else {
            /**
             * 0.券码不正确
             * 1.优惠券已经发放完
             * 2.不能再领，数量达到上限
             * 3.可以正常添加
             * 4.对优惠的数量处理
             */
            Integer state = couponService.exchangeService(userId, exchangeCodeBO.getCode());
            if (state == 0) {
                return BaseRespVO.error("优惠券不正确", 742);
            } else if (state == 1) {
                return BaseRespVO.error("优惠券发放完", 740);
            } else if (state == 2) {
                return BaseRespVO.error("领券次数达上限", 740);
            } else {
                return BaseRespVO.ok(0);
            }
        }
    }


}
